package GUI;

import java.util.ResourceBundle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

import Data.*;

public abstract class AttributeMethod extends JPanel {
	protected final Color BACKGROUND = new Color(225, 225, 225);
	private final int TEXT_FIELD_LENGTH = 15;
	
	ResourceBundle lang;
	Options options;
	Log log;
	Classes classes;
	
	JPanel me = this;
	ClassFrame classFrame;
	protected JLabel typeLabel;
	protected JTextField nameTextField;
	protected JButton deleteButton;
	
	public AttributeMethod() {
		options = Options.getInstance();
		log = Log.getInstance();
		lang = Options.getLanguage();
		classes = Classes.getInstance();
		
		setLayout(new FlowLayout(FlowLayout.LEFT));
		typeLabel= new JLabel();
		//add(typeLabel);
		
		
		nameTextField = new JTextField(lang.getString("AttributeMethod.nameTextField.defaultText"), TEXT_FIELD_LENGTH);
		add(nameTextField);
		nameTextField.selectAll();
		nameTextField.setBorder(null);
		nameTextField.setBackground(Color.WHITE);
		nameTextField.addMouseListener(new MouseHandler());
		nameTextField.addKeyListener(new KeyHandler());
		
		deleteButton = new JButton(lang.getString("AttributeMethod.deleteButton.text"));
		add(deleteButton);
		deleteButton.addMouseListener(new MouseHandler());
		deleteButton.setBorder(null);
		deleteButton.setBackground(new Color(200, 200, 200));
		
		this.setBackground(BACKGROUND);
		this.setBorder(null);
		
		addComponentListener(new ComponentHandler());
		
	}
	
	public AttributeMethod(String text, ClassFrame classFrame) {
		this();
		nameTextField.setText(text);
		this.classFrame = classFrame;
	}

	
	public AttributeMethod(AttributeMethod am, ClassFrame classFrame) {
		this(am.nameTextField.getText(), classFrame);
		System.out.print("123");
		nameTextField.setBackground(am.nameTextField.getBackground());
		nameTextField.setEditable(am.nameTextField.isEditable());
	}
	
	public JTextField getNameReference() {
		return nameTextField;
	}
	
	public void actionPerformed(ActionEvent event) {
		//JOptionPane.showMessageDialog(null, "123dfhdfh");
		//event.
	}
	
	private class KeyHandler extends KeyAdapter {
		public void keyPressed(KeyEvent event) {
			if (event.getKeyCode() == 10) {
				nameTestField_EnterKeyPressed();
			}
		}
	}
	
	private class MouseHandler extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			if (event.getSource() == nameTextField) {
				if (event.getClickCount()>= 2 && !nameTextField.isEditable()) {
					nameTextField_actived();
				}
			} else {
				deleteButton_Clicked();
			}
		}
	}
	
	private class ComponentHandler extends ComponentAdapter {
		public void componentResized(ComponentEvent event) {
		}
	}
	
	public abstract void nameTestField_EnterKeyPressed();
	
	public void nameTextField_actived() {
		nameTextField.setEditable(true);
		nameTextField.selectAll();
		nameTextField.setBackground(Color.WHITE);
		nameTextField.setName(nameTextField.getText());
	}
	
	public abstract void deleteButton_Clicked();
}
